package Java实训.第三天JDK特性流;

import java.util.ArrayList;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

public class Num1 {
    /*
     * 给一个整数集合，分别求集合中的偶数和与奇数和
     * */
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Random rm = new Random();
        for (int i = 0; i < 10; i++) {
            list.add(rm.nextInt(10));
        }
        System.out.println("该集合为: " + list);
        Optional<Integer> ouresault = list.stream().filter(t -> t % 2 == 0).reduce((t1, t2) -> t1 + t2);
        Integer ou = ouresault.get();
        System.out.println("偶数和为: " + ou);
        Integer ji = list.stream().filter(t -> t % 2 != 0).collect(Collectors.summingInt(t -> t));
        System.out.println("奇数和为: " + ji);
    }
}
